/*! ******************************************************************************
 *
 * Created on 2022年3月8日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.xul.swt.tags;

import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.components.XulListitem;
import org.pentaho.ui.xul.containers.XulListbox;
import org.pentaho.ui.xul.dom.Element;
import org.pentaho.ui.xul.swt.SwtElement;

public class SwtListitem extends SwtElement implements XulListitem {

    String label = null;

    Object value = null;

    XulComponent parent;

    boolean isSelected = false;

    public SwtListitem(Element self, XulComponent parent, XulDomContainer container, String tagName) {
        super(tagName);
        this.parent = parent;
    }

    public void setLabel(String text) {

        XulComponent p = getParent() != null ? getParent() : parent;
        label = text;

        if (value == null) {
            setValue(label);
        }

        ((SwtListbox)p).updateLabel(this);
    }

    public String getLabel() {

        return label;

    }

    public Object getValue() {

        return value;

    }

    public boolean isSelected() {

        return isSelected;

    }

    public void setSelected(boolean selected) {
        isSelected = selected;

        if (value != null) {

            ((XulListbox)parent).setSelectedItem(value);

        }
    }

    public void setValue(Object value) {

        this.value = value;

        if (label == null) {

            label = value.toString();

        }

        XulComponent p = getParent() != null ? getParent() : parent;
        ((SwtListbox)p).addItem(value);
    }

}
